package spill;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.Timer;

public class Engine implements MouseListener, ActionListener {
	private ArrayList<Drawable> itemList;
	private ArrayList<Money> moneyList;
	private ArrayList<Bucket> bucketList;
	private Money templateMoney;
	private boolean bucketPushed;
	private Bucket currentBucket;
	private long time;
	private Screen screen;
	private static String[] names = new String[] {"Fagkom", "Labamba", "Backup", "HS"};
	private Font defFont;
	private Timer timer;
	private Student student;
	private Hand hand;
	
	public Engine(Screen screen) {
		timer = new Timer(15, this);
		this.screen = screen;
		screen.addMouseListener(this);
		screen.setEngine(this);
		templateMoney = new Money(this);
		defFont = new Font("Monospaced", Font.PLAIN, 10);
		reset();
	}
	public void reset() {
		itemList = new ArrayList<Drawable>();
		moneyList = new ArrayList<Money>();
		bucketList = new ArrayList<Bucket>();
		Bedkom bedkom = new Bedkom(this);
		itemList.add(bedkom);
		for (int i = 0; i < names.length; i++) {
			Bucket b = new Bucket(names[i], this);
			b.setRestCoord(570, 20+i*100);
			add(b);
		}
		student = new Student(this);
		itemList.add(student);
		hand = new Hand(this, student);
		itemList.add(hand);
		itemList.add(new Background(this));
		currentBucket = bucketList.get(0);
		time = 0;
		bucketPushed = false;
		timer.start();
	}
	public void add(Money m) {
		itemList.add(m);
		moneyList.add(m);
	}
	public void add(Bucket b) {
		itemList.add(b);
		bucketList.add(b);
	}
	public void remove(Money m) {
		itemList.remove(m);
		moneyList.remove(m);
	}
	public void remove(Bucket b) {
		itemList.remove(b);
		bucketList.remove(b);
	}
	public void remove(Student s) {
		itemList.remove(s);
	}
	public void tick() {
		time++;
		moveItems();
		if (bucketPushed) {
			Point p = screen.getMousePosition();
			if (p != null) {
				currentBucket.move(p.x, p.y);
			}
			currentBucket.eat(moneyList, this);
		}
		screen.repaint();
		checkLevels();
		morphNewItems();
	}
	public void checkLevels() {
		for (int i = 0; i < bucketList.size(); i++) {
			bucketList.get(i).consume(time);
			if (!bucketList.get(i).isAlive()) {
				timer.stop();//stop game
				new Highscore(time, this);
				break;
			}
		}
	}
	public void drawItems(Graphics g) {
		g.setColor(Color.white);
		g.clearRect(0, 0, screen.getWidth(), screen.getHeight());
		for (int i = 0; i < itemList.size(); i++) {
			g.setFont(defFont);
			itemList.get(i).draw(g);
		}
	}
	public void moveItems() {
		for (int i = 0; i < moneyList.size(); i++) {
			moneyList.get(i).move();
		}
		student.move();
	}
	private void createMoney() {
		Money newMoney = new Money(templateMoney, this);
		newMoney.setX((int)(Math.random()*(screen.getWidth()-100)));
		add(newMoney);
	}
	public void morphNewItems() {
		if (Math.random() < 0.04) {
			createMoney();
		}
		if (!student.isInUse() && Math.random() < 0.005) {
			student.reset();
			itemList.add(student);
		}
	}
	public void makeItRain() {
		for (int i = 0; i < 15; i++) {
			createMoney();
		}
	}

	public void mouseClicked(MouseEvent e) {
		if (e.getY() < 100) {
			hand.strike(e.getX());
		}
	}
	public void mouseEntered(MouseEvent arg0) {
		
	}
	public void mouseExited(MouseEvent arg0) {
		
	}
	public void mousePressed(MouseEvent e) {
		for (int i = 0; i < bucketList.size(); i++) {
			if (bucketList.get(i).isClicked(e.getX(), e.getY())) {
				this.bucketPushed = true;
				this.currentBucket = bucketList.get(i);
			}
		}
		if (e.getY() < 100 && !bucketPushed) {
			hand.strike(e.getX());
		}
	}
	public void mouseReleased(MouseEvent arg0) {
		bucketPushed = false;
		currentBucket.rest();
	}
	public Font getDefFont() {
		return defFont;
	}
	public Font getFont(double sizeM) {
		return new Font("Monospaced", Font.PLAIN, (int)(sizeM*defFont.getSize()));
	}
	public void actionPerformed(ActionEvent arg0) {
		this.tick();
	}
	public long getTime() {
		return time;
	}
	
	public static void main(String[] args) {
		JFrame frame = new JFrame("...");
		Screen screen = new Screen();
		frame.getContentPane().add(screen);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setSize(700, 600);
		frame.setResizable(false);
		frame.setVisible(true);
		new Engine(screen);
	}
}

